# from server import baseServerHandler
from server import *
from webserver import *

'''
三个主要界面：
main界面：		选择已连接的客户端
connet界面：	选择解析器或跳转客户端
data界面：		显示数据
访问格式
192.168.1.2:8000/
main/192.168.1.2:8000/hj212/其他参数
'''
# HTML前缀
main_html_prefix = '''
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>HJ212客户端调试工具</h1>
<h2>已连接的客户端列表</h2>
<blockquote>
    <p>选择一个已连接的客户端</p>
'''
# HTML后缀
main_html_suffix = '''
</blockquote>
</body>
</html>
'''
# HTML无连接
main_html_no_connet = '''
	<p>无连接...</p>
'''
client_list_header = '''
	<table>
        <thead>
            <tr>
                <th><strong>Server</strong></th>
                <th><strong>Client</strong></th>
                <th><strong>Client-Mac</strong></th>
                <th><strong>Alias</strong></th>
                <th><strong>选择</strong></th>
            </tr>
        </thead>
        <tbody></tbody>
	</table>
'''
client_list_items = '''
            <tr>
                <td>&lt;Server&gt;</td>
                <td>&lt;Client&gt;</td>
                <td>&lt;Client-Mac&gt;</td>
                <td>Alias</td>
                <td><a href="link"target="_blank">查看</a></td>
            </tr>
'''


def HTML_update(html, cycle):
	string = '''\n\t<meta http-equiv="refresh" content="''' + str(cycle) + '"\n'
	index = html.index("<head>") + len("<head>")
	html = html[0 : index] + string + html[index : ]
	return html


def getHtml_HJ212_2017(handle, request_msg):
	print("host: ", handle.args["host"])
	print("hj212server: ", handle.args["hj212server"])
	
	webserver = handle.args["host"]
	server = handle.args["hj212server"]

	html = main_html_prefix
	if request_msg == None:		# main界面
		if len(server.getClientList()) <= 0:		# 无客户端
			html += main_html_no_connet
		else :	 					# 需要生成客户端列表
			client_list = ""
			items = ""
			for key in server.getClientList():
				host = server.host
				if host == "":
					host = "localhost"
				addr = key[0] + ":" + str(key[1])
				link = webserver.host + ":" + str(webserver.port) + "/" + addr + "/"
				items = client_list_items
				items = items.replace("Server", host + ":" + str(server.port), 1)
				items = items.replace("Client", addr, 1)
				items = items.replace("link", link, 1)
				client_list += items
			client_list = client_list_header.replace("<tbody></tbody>", "<tbody>" + client_list + "</tbody>")
			html += client_list
	html += main_html_suffix
	# print(html)
	return HTML_update(html, 1)				# 1秒更新一次html


class HJ212ServerHandler(baseServerHandler):        # 创建HJ212服务器

	def recvDataDispose(self, data):
		print(data.decode("ascii"))


if __name__=="__main__":		# 测试程序
	server = TCPServer(port=8002, Handler=HJ212ServerHandler)
	web = WebServer(port=8001, getHtml=getHtml_HJ212_2017, HandleArgs={"hj212server":server})
	while True:
		cmd = input("cmd->")
		if cmd == "disconnet":
			server.disconnet()
		elif cmd == "start":
			server.start()
		elif cmd == "print":
			print("server.client:")
			print(server.client)
		elif cmd == "delete":
			print("del server")
			del server
		elif cmd == "getref":
			print(sys.getrefcount(server))
		elif cmd == "stop":
			server.stop()
			print("server.stop()")
		elif cmd == "client":
			print(server.getClientList())
		elif cmd == "host-type":
			print(server.getClientList()[0])
			print(type(server.getClientList()[0]))
			print(server.getClientList()[0][0])
			print(type(server.getClientList()[0][0]))
			print(server.getClientList()[0][1])
			print(type(server.getClientList()[0][1]))







